namespace QMVnSoft.SystemForms
{
    using QMVnSoft;
    using QMVnSoft.Controls;
    using QMVnSoft.DAL;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormMerge2Code : FormControl
    {
        private BtnControl btnCancel;
        private BtnControl btnOk;
        private IContainer components;
        private GBoxControl gBoxControl01;
        private GBoxControl gBoxControl02;
        private string is_LocalFieldName;
        private object is_LocalOldCode;
        private string is_LocalTableName;
        private TextSearch txtNewCode;
        private TextControl txtOldCode;

        public FormMerge2Code()
        {
            this.InitializeComponent();
        }

        public FormMerge2Code(string ps_TableName, string ps_FieldName, object ps_OldCode)
        {
            this.InitializeComponent();
            this.LocalTableName = ps_TableName;
            this.LocalFieldName = ps_FieldName;
            this.LocalOldCode = ps_OldCode;
            this.txtNewCode.TableName = ps_TableName;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.OnDo();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            //ComponentResourceManager manager = new ComponentResourceManager(typeof(FormMerge2Code));
            this.gBoxControl01 = new GBoxControl();
            this.txtOldCode = new TextControl();
            this.btnCancel = new BtnControl();
            this.btnOk = new BtnControl();
            this.gBoxControl02 = new GBoxControl();
            this.txtNewCode = new TextSearch();
            this.gBoxControl01.SuspendLayout();
            this.gBoxControl02.SuspendLayout();
            base.SuspendLayout();
            this.gBoxControl01.Controls.Add(this.txtOldCode);
            this.gBoxControl01.Font = new Font("Arial", 8.25f);
            this.gBoxControl01.Location = new Point(12, 12);
            this.gBoxControl01.Name = "gBoxControl01";
            this.gBoxControl01.Size = new Size(0x14b, 0x3a);
            this.gBoxControl01.TabIndex = 0;
            this.gBoxControl01.TabStop = false;
            this.gBoxControl01.Text = "Gộp phát sinh của mã";
            this.txtOldCode.CharacterCasing = CharacterCasing.Upper;
            this.txtOldCode.CheckTextSearched = false;
            this.txtOldCode.DisabledForeColor = Color.Blue;
            this.txtOldCode.Enabled = false;
            this.txtOldCode.Font = new Font("Arial", 8.25f);
            this.txtOldCode.Location = new Point(80, 0x15);
            this.txtOldCode.MaxLength = 0x20;
            this.txtOldCode.Name = "txtOldCode";
            this.txtOldCode.OldValue = null;
            this.txtOldCode.Size = new Size(170, 20);
            this.txtOldCode.TabIndex = 1;
            this.txtOldCode.UserTextLength = AppInstance.TextControlLength.ItemCode;
//            this.txtOldCode.Value = null;//manager.GetObject("txtOldCode.Value");
            this.btnCancel.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            //this.btnCancel.DialogResult = DialogResult.Cancel;
            this.btnCancel.Font = new Font("Arial", 8.25f);
            this.btnCancel.Location = new Point(180, 0x99);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(0x4b, 0x17);
            this.btnCancel.TabIndex = 3;
            this.btnCancel.Text = "&Hủy bỏ";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            this.btnOk.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.btnOk.Font = new Font("Arial", 8.25f);
            this.btnOk.Location = new Point(0x63, 0x99);
            this.btnOk.Name = "btnOk";
            this.btnOk.Size = new Size(0x4b, 0x17);
            this.btnOk.TabIndex = 2;
            this.btnOk.Text = "&Chấp nhận";
            this.btnOk.UseVisualStyleBackColor = true;
            this.btnOk.Click += new EventHandler(this.btnOk_Click);
            this.gBoxControl02.Controls.Add(this.txtNewCode);
            this.gBoxControl02.Font = new Font("Arial", 8.25f);
            this.gBoxControl02.Location = new Point(12, 0x4c);
            this.gBoxControl02.Name = "gBoxControl02";
            this.gBoxControl02.Size = new Size(0x14b, 0x3a);
            this.gBoxControl02.TabIndex = 1;
            this.gBoxControl02.TabStop = false;
            this.gBoxControl02.Text = "vào phát sinh của mã";
            this.txtNewCode.CharacterCasing = CharacterCasing.Upper;
            this.txtNewCode.CheckTextSearched = false;
            this.txtNewCode.DisabledForeColor = Color.Blue;
            this.txtNewCode.Font = new Font("Arial", 8.25f);
            this.txtNewCode.Location = new Point(80, 0x15);
            this.txtNewCode.MaxLength = 0x20;
            this.txtNewCode.Name = "txtNewCode";
            this.txtNewCode.OldValue = null;
            this.txtNewCode.Size = new Size(170, 20);
            this.txtNewCode.TabIndex = 1;
            this.txtNewCode.TableName = "";
            this.txtNewCode.UserTextLength = AppInstance.TextControlLength.ItemCode;
//            this.txtNewCode.Value = null;//manager.GetObject("txtNewCode.Value");
            base.AcceptButton = this.btnOk;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.CancelButton = this.btnCancel;
            base.ClientSize = new Size(0x163, 0xbc);
            base.ControlBox = false;
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnOk);
            base.Controls.Add(this.gBoxControl02);
            base.Controls.Add(this.gBoxControl01);
            //base.FormBorderStyle = FormBorderStyle.FixedDialog;
            base.Name = "FormMerge2Code";
            base.ShowInTaskbar = false;
            this.Text = "";
            this.gBoxControl01.ResumeLayout(false);
            this.gBoxControl01.PerformLayout();
            this.gBoxControl02.ResumeLayout(false);
            this.gBoxControl02.PerformLayout();
            base.ResumeLayout(false);
        }

        private void MergeCode()
        {
            if (MessageBox.Show("Bạn có muốn gộp mã không?", AppInstance.SoftwareName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (((this.LocalFieldName == "TK") || (this.LocalFieldName == "MA_NH_DT")) || ((this.LocalFieldName == "MA_NH_VT") || (this.LocalFieldName == "MA_NH_TS")))
                {
                    DataTable table = AppDataAccess.CheckInUsed(this.LocalFieldName, this.LocalTableName);
                    DataRow row = table.Rows.Find(this.LocalTableName);
                    if (row != null)
                    {
                        table.Rows.Remove(row);
                    }
                    if (table.Rows.Count != 0)
                    {
                        FormCheckDeleted deleted = new FormCheckDeleted();
                        deleted.LocalTable = table;
                        deleted.ShowDialog();
                        return;
                    }
                }
                AppDataAccess.MergeCode(this.LocalFieldName, this.txtOldCode.Value, this.txtNewCode.Value);
            }
        }

        private void OnDo()
        {
            this.MergeCode();
            base.Close();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Control | Keys.Return))
            {
                this.OnDo();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private string LocalFieldName
        {
            get
            {
                return this.is_LocalFieldName;
            }
            set
            {
                this.is_LocalFieldName = value;
            }
        }

        private object LocalOldCode
        {
            get
            {
                return this.is_LocalOldCode;
            }
            set
            {
                this.is_LocalOldCode = value;
                this.txtOldCode.Value = value;
            }
        }

        private string LocalTableName
        {
            get
            {
                return this.is_LocalTableName;
            }
            set
            {
                this.is_LocalTableName = value;
            }
        }
    }
}

